'***When writing BMX Native Script Functions, make sure to pop the whole stack when a function is called.
'***This helps to prevent stack overflows (and overrun hacks)
Type LQScript
	Global AllScripts:TList = CreateList() 'of LQScript Instances
	Field fileName:String
	Field functions:TList 'of String
	
	Function Init()
		'get all the lua files in the script directory, and put them and all their functions into the AllScripts list.
		Local dir = ReadDir(ProjectFolder + "/Assets/Scripts")
		Repeat
			Local file:String = NextFile(dir)
			If file = "" Then Exit
			If file = "." Or file = ".." Then Continue
			If ExtractExt(file) = "lua" Then
				New LQScript.Create(StripAll(file))
			EndIf
		Forever
		CloseDir dir
				
		'---NOTE---:
		'the script functions will be added [to their comboboxes] when the script file is selected in it's combobox.
	End Function
	
	Function GetScriptByFileName:LQScript(fileName:String)
		For Local script:LQScript = EachIn AllScripts
			If script.fileName = fileName Then Return script
		Next
		Return Null
	End Function
	
	Function GetFunctions:TList(fileName:String) 'returns list of strings (function names)
		For Local script:LQScript = EachIn AllScripts
			If script.fileName = fileName Then Return script.functions
		Next
		Return Null
	End Function
	
	Method Create:LQScript(newFileName:String)
		fileName = newFileName
		functions = CreateList()
		GetMyFunctions()
		LQScript.AllScripts.AddLast(Self)
		AddGadgetItem(ZoneCbbStartScriptFile,fileName,0,-1,"",Self)
		Return Self
	End Method
	
	Method GetMyFunctions()
		'get and store all the functions defined in this file into the functions list.
		'***Add this functionality!!!
	End Method
	
	Method LoadIntoBox()
		Local file:TStream = LittleEndianStream(ReadFile(projectFolder + "/Assets/Scripts/" + fileName + ".lua"))
		If Not file Then
			Error("Could Not Read Script File!")
		EndIf
		SetTextAreaText(TxtScript,file.ReadString(file.Size()))
		file.Close()
	End Method
	
	Method SaveFromBox()
		Local file:TStream = LittleEndianStream(WriteFile(ProjectFolder + "/Assets/Scripts/" + fileName + ".lua"))
		If Not file Then
			Error("Could Not Save Script File!")
		EndIf
		file.WriteString(TextAreaText(TxtScript))
		file.Close()
		GetMyFunctions()
	End Method
End Type
